<?php
/**
 * Created by PhpStorm.
 * User: bzg
 * Date: 2019/11/28
 * Time: 15:27
 */

namespace App\Extend\SysExtend\Database;

use App\Extend\SysExtend\Helper;
use EasySwoole\ORM\Db\Connection;
use EasySwoole\ORM\DbManager;

/**
 * 数据库连接
 * Class Database
 * @package App\Extend\SysExtend\Database
 */
class Database
{
    /**
     * 注册数据库连接池
     * @throws \EasySwoole\Pool\Exception\Exception
     * @throws \Throwable
     */
    public static function registerPool()
    {
        $db_conf = Helper::config('database');
        $cur_conf = $db_conf[$db_conf['database']];
        // 注册数据库连接
        $config = new \EasySwoole\ORM\Db\Config();
        $config->setDatabase($cur_conf['db_name']);
        $config->setUser($cur_conf['user']);
        $config->setPassword($cur_conf['password']);
        $config->setHost($cur_conf['host']);
        //连接池配置
        $config->setGetObjectTimeout($db_conf['pool']['time_out']); //设置获取连接池对象超时时间
        $config->setIntervalCheckTime($db_conf['pool']['life_time']); //设置检测连接存活执行回收和创建的周期
        $config->setMaxIdleTime($db_conf['pool']['max_idle_time']); //连接池对象最大闲置时间(秒)
        $config->setMaxObjectNum($db_conf['pool']['max_num']); //设置最大连接池存在连接对象数量
        $config->setMinObjectNum($db_conf['pool']['min_num']); //设置最小连接池存在连接对象数量
        DbManager::getInstance()->addConnection(new Connection($config));
    }
}